<template>
  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <el-table
      :data="PermissionList"
      row-key="id"
      :expand-row-keys="['1']"
      border
      stripe
    >
      <el-table-column label="名称" prop="name"> </el-table-column>
      <el-table-column label="权限值" prop="code"> </el-table-column>
      <el-table-column label="跳转权限值"> </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-plus"
            :disabled="row.level >= 4"
            @click="addpower(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="updatePermission(row)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="delpermissionItem(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加菜单" :visible.sync="dialogIsshow" width="70%">
      <el-form
        :model="powerFrom"
        ref="PowerFromRef"
        :rules="powerFromRules"
        label-width="120px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="powerFrom.name"></el-input>
        </el-form-item>
        <el-form-item label="功能权限值" prop="code">
          <el-input v-model="powerFrom.code"></el-input>
        </el-form-item>
        <el-form-item label="路由跳转权限值" v-show="powerFrom.type === 2">
          <el-input v-model="powerFrom.toCode"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="dialogIsshow = false">关闭</el-button>
        <el-button type="primary" @click="addPowerItem">确定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  reqPermissionList,
  addItemPower,
  updatePower,
  delPowerItem
} from "@/api/aci_permission";
export default {
  name: "Permission",
  data() {
    return {
      PermissionList: [],
      dialogIsshow: false,
      powerFrom: {
        id: "",
        name: "",
        code: "",
        type: "",
        level: "",
        pid: "",
        toCode: ""
      },
      powerFromRules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        code: [{ required: true, message: "请输入权限值", trigger: "blur" }]
      }
    };
  },
  computed: {},
  mounted() {
    this.getPermissionList();
  },
  methods: {
    async getPermissionList() {
      let re = await reqPermissionList();
      this.PermissionList = re.children;
    },
    addpower(row) {
      //展开时初始化数据
      this.powerFrom = {
        id: "",
        name: "",
        code: "",
        type: "",
        level: "",
        pid: "",
        toCode: ""
      };
      this.powerFrom.type = row.level >= 3 ? 2 : 1;
      this.powerFrom.level = +row.level + 1;
      this.powerFrom.pid = row.id;
      this.dialogIsshow = true;
    },
    async addPowerItem() {
      this.$refs.PowerFromRef.validate(async bol => {
        if (bol) {
          if (this.powerFrom.id) {
            await updatePower(this.powerFrom);
          } else {
            await addItemPower(this.powerFrom);
          }
          this.$message({
            type: "success",
            message: "数据修改成功"
          });
          this.getPermissionList();
          this.dialogIsshow = false;
        }
      });
    },
    updatePermission(row) {
      this.powerFrom = Object.assign(this.powerFrom, row);
      this.dialogIsshow = true;
    },
    async delpermissionItem(id) {
      this.$confirm("此操作将永久删除该菜单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await delPowerItem(id);
          this.$message({
            type: "success",
            message: "数据删除成功"
          });
          this.getPermissionList();
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "取消删除"
          });
        });
    }
  }
};
</script>

<style></style>
